package com.zg.ktcomwan.vm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.zg.ktcomwan.datasource.ProjectDataSource
import com.zg.ktcomwan.datasource.SquareDataSource
import com.zg.ktcomwan.repository.ProjectRepositroty
import com.zg.ktcomwan.repository.SquareRepositroty

/***
 * @Author:      zxd
 * @CreateDate:  2021/6/2 15:09
 * @Description:
 */
class SquareViewModel() : ViewModel() {

    //下面是通过flow的形式获取数据
    private val repository = SquareRepositroty()

    var articleList =
        Pager(
            config = PagingConfig(
                pageSize = 20,
                enablePlaceholders = false,
                initialLoadSize = 20
            ),
            pagingSourceFactory = {
                SquareDataSource(repository)
            }
        ).flow.cachedIn(viewModelScope)/*.asLiveData(EmptyCoroutineContext, 1000)*/
    //加上asLiveData就需要使用 netViewModel.articleList.observe() 来获取数据
}